home *** CD-ROM | disk | FTP | other *** search
/ Czech Logic, Card & Gambling Games / Logické hry.iso / hry / Fish Fillets / script / share / prog_goanim.lua < prev    next >
Text File  |  2005-07-16  |  3KB  |  122 lines

  1.  
  2. -- -----------------------------------------------------------------
  3. -- setanim() and gonanim()
  4. -- -----------------------------------------------------------------
  5. -- Example: setanim(model, "a3d3a4a5a6a7d2a6a5a4a3")
  6.  
  7. function resetanim(model)
  8.     model.anim_delay = 0
  9.     model.anim_pos = 1
  10.     model.anim_label = 1
  11. end
  12.  
  13. function endanim(model)
  14.     -- returns true for just finished animation
  15.     local result = false
  16.     if model.anim_pos > string.len(model.anim) and model.anim ~= "" then
  17.         result = true
  18.     end
  19.     return result
  20. end
  21.  
  22. function setanim(model, anim_string)
  23.     resetanim(model)
  24.     model.anim = anim_string
  25. end
  26.  
  27. local function anim_getSymbol(model)
  28.     return string.sub(model.anim, model.anim_pos, model.anim_pos)
  29. end
  30. local function anim_incPos(model)
  31.     model.anim_pos = model.anim_pos + 1
  32. end
  33.  
  34. local function anim_var(model)
  35.     -- var := \[[a-zA-Z]+\]
  36.     local istart, iend, str_var = string.find(model.anim,
  37.             "%[(%w+)%]", model.anim_pos)
  38.     if istart == nil then
  39.         error("SCRIPT_ERROR empty anim_var; anim="..model.anim)
  40.     end
  41.     return str_var
  42. end
  43.  
  44. local function anim_number(model)
  45.     -- N := -?[0-9]+ | \?N.N
  46.     local symbol = anim_getSymbol(model)
  47.     if symbol == "?" then
  48.         anim_incPos(model)
  49.         local int1 = anim_number(model)
  50.         anim_incPos(model)
  51.         local int2 = anim_number(model)
  52.         return randint(int1, int2)
  53.     end
  54.  
  55.     local istart, iend, str_number = string.find(model.anim,
  56.             "(%-?%d+)", model.anim_pos)
  57.     local number = 0
  58.     if istart == nil then
  59.         print("SCRIPT_WARNING empty number; anim="..model.anim)
  60.     else
  61.         model.anim_pos = iend + 1
  62.         number = tonumber(str_number)
  63.         if number == nil then
  64.             error("SCRIPT_ERROR bad number; str_number="..str_number)
  65.         end
  66.     end
  67.     return number
  68. end
  69.  
  70. local function anim_next(model)
  71.     -- Process next symbol, uppercase letters are not blocking.
  72.     local symbol = "A"
  73.     while string.lower(symbol) ~= symbol and model.anim_pos <= string.len(model.anim) do
  74.         symbol = anim_getSymbol(model)
  75.         anim_incPos(model)
  76.         switch(string.lower(symbol)){
  77.             ["d"] = function()
  78.                 model.anim_delay = anim_number(model)
  79.                 if model.anim_delay < 0 then
  80.                     model.anim_delay = 0
  81.                     symbol = "D"
  82.                 end
  83.             end,
  84.             ["a"] = function()
  85.                 model.afaze = anim_number(model)
  86.             end,
  87.             ["s"] = function()
  88.                 local var = anim_var(model)
  89.                 anim_incPos(model)
  90.                 local value = anim_number(model)
  91.                 model[var] = value
  92.             end,
  93.             ["l"] = function()
  94.                 model.anim_label = model.anim_pos
  95.             end,
  96.             ["g"] = function()
  97.                 model.anim_pos = model.anim_label
  98.             end,
  99.             ["r"] = function()
  100.                 model.anim_pos = 1
  101.             end,
  102.             default = function()
  103.                 error("SCRIPT_ERROR unknown anim symbol; symbol="..symbol.."; pos="..model.anim_pos.."; anim="..model.anim)
  104.             end,
  105.         }
  106.     end
  107. end
  108.  
  109.  
  110. function goanim(model)
  111.     -- goanim process next anim command
  112.     if model.anim_delay > 0 then
  113.         model.anim_delay = model.anim_delay - 1
  114.     elseif model.anim_pos > string.len(model.anim) then
  115.         model.anim = ""
  116.         model.anim_pos = 1
  117.     else
  118.         anim_next(model)
  119.     end
  120. end
  121.  
  122.